#include <bits/stdc++.h>
using namespace std;
const int N = 1005;

int n, m;
int fa[N];
int find(int x)
{
    if (fa[x] == x)
        return x;
    fa[x] = find(fa[x]);
    return fa[x];
}
bool visit[N];
int sum;
int main()
{
    int a, b;
    while (true)
    {
        scanf("%d", &n);
        if (n == 0)
            break;
        scanf("%d", &m);
        for (int i = 1; i <= n; i++)
        {
            fa[i] = i;
        }
        for (int i = 0; i < m; i++)
        {
            scanf("%d%d", &a, &b);
            fa[find(a)] = find(b);
        }
        sum = 0;
        memset(visit, false, sizeof(visit));
        for (int i = 1; i <= n; i++)
        {
            if (!visit[find(i)])
            {
                sum++;
                visit[find(i)] = true;
            }
        }
        printf("%d\n", sum - 1);
    }
    return 0;
}